/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import cart.ShoppingCart;
import cart.ShoppingCartItem;
import com.opensymphony.xwork2.ActionContext;
import dao.CategoryDAO;
import dto.Category;
import dto.Product;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.apache.commons.logging.Log;

/**
 *
 * @author duonghung1369
 */
public class CategoryController extends BaseController {

    private ShoppingCart cart;
    private ShoppingCartItem item;
    private Product product;
    private InputStream inputStream;
    private Map session;
    private final String CART = "cart";
    private Log log;
    private CategoryDAO categoryDAO;

    public CategoryController() {
        super();
        categoryDAO = new CategoryDAO(Category.class);
    }

    public String add_cart() {
        try {
            session = ActionContext.getContext().getSession();
            Object obj = session.get(CART);
            if (obj != null) {
                setCart((ShoppingCart) obj);
            } else {
                setCart(new ShoppingCart());
            }

            setItem(new ShoppingCartItem());
            getItem().setItem(getProduct());
            getCart().addToCart(getItem());

            // store cart to session
            session.put(CART, getCart());

            setInputStream(new ByteArrayInputStream(String.valueOf(getCart().getNumber_of_items()).getBytes("UTF-8")));
        } catch (UnsupportedEncodingException ex) {
            log.error("CategoryController.add_cart()", ex);
        }

        return SUCCESS;
    }

    /**
     * @return the cart
     */
    public ShoppingCart getCart() {
        return cart;
    }

    /**
     * @param cart the cart to set
     */
    public void setCart(ShoppingCart cart) {
        this.cart = cart;
    }

    /**
     * @return the item
     */
    public ShoppingCartItem getItem() {
        return item;
    }

    /**
     * @param item the item to set
     */
    public void setItem(ShoppingCartItem item) {
        this.item = item;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * @return the inputStream
     */
    public InputStream getInputStream() {
        return inputStream;
    }

    /**
     * @param inputStream the inputStream to set
     */
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String view_cart() {
        session = ActionContext.getContext().getSession();
        Object obj = session.get(CART);
        if (obj != null) {
            cart = (ShoppingCart) obj;
        } else {
            cart = new ShoppingCart();
        }
        return SUCCESS;
    }

    public String checkout() {
        return SUCCESS;
    }

   public String getAllCategories() {
       categoryDAO.openSession();

       return SUCCESS;
   }
}
